#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<vector>

using namespace std;

class Solution
{
public:
    void duplicateZeros(vector<int>& arr)
    {
        int cur = -1;
        int des = -1;
        //int len = arr.size();
        while (des < (int)arr.size() -1)
        {
            cur++;
            if (arr[cur] == 0)
                des += 2;
            else
                des++;
        }

        if (des == arr.size())
        {
            cur--;
            arr[des - 1] = 0;
            des -= 2;
        }

        while (cur >= 0)
        {
            if (arr[cur] == 0)
            {
                arr[des] = 0;
                arr[des - 1] = 0;
                des -= 2;
            }
            else
            {
                arr[des] = arr[cur];
                des--;
            }
            cur--;
        }
    }
};

class Solution2 {
public:
    bool isHappy(int n) {
        int fast = n;
 
        int j = 2;
        while (j--)
        {
            int tmp1 = 0;
            while (fast)
            {
                tmp1 += pow(fast % 10, 2);
                fast /= 10;
            }
            fast = tmp1;
        }


        int slow = n;
        int tmp2 = 0;
        while (slow)
        {
            tmp2 += pow(slow % 10, 2);
            slow /= 10;
        }
        slow = tmp2;

        while (fast != slow)
        {
            int ret = 0;
            while (slow)
            {
                ret += pow(slow % 10, 2);
                slow /= 10;
            }
            slow = ret;
            int i = 2;
            while (i--)
            {
                ret = 0;
                while (fast)
                {
                    ret += pow(fast % 10, 2);
                    fast /= 10;
                }
                fast = ret;
            }
        }
        if (slow == 1)
            return true;
        else
            return false;
    }
};

int main()
{
   // Solution s1;
   //// vector<int> v = { 1,2,3 };
   // vector<int> v = { 1,0,2,3,0,4,5,0 };

   // s1.duplicateZeros(v);
   // for (auto e:v)
   // {
   //     cout << e<<" ";
   // }
   // cout << endl;
    Solution2 s1;
    bool ret=s1.isHappy(19);
    cout << ret;

    return 0;
}